package id.co.shuba.spamobile.widget;

import id.co.shuba.spamobile.R;
import id.co.shuba.spamobile.model.MenuInfo;

import java.util.ArrayList;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MenuAdapter extends BaseAdapter
{
	private ArrayList<MenuInfo> menuList;	
	private Activity activity;

	public MenuAdapter(Activity activity,ArrayList<MenuInfo> menuList) {
		super();
		this.menuList = menuList;		
		this.activity = activity;
	}

	public int getCount() {		
		return menuList.size();
	}

	public String getItem(int position) { 
		return menuList.get(position).getName();
	}
	
	public MenuInfo getMenu(int position) {
		return menuList.get(position);
	}

	public long getItemId(int position) {
		return 0;
	}
	
	public static class ViewHolder
	{
		public ImageView imgViewFlag;
		public TextView txtViewTitle;
	}

	public View getView(int position, View convertView, ViewGroup parent) {		
		ViewHolder view;
		LayoutInflater inflator = activity.getLayoutInflater();

		if(convertView==null)
		{
			view = new ViewHolder();
			convertView = inflator.inflate(R.layout.menu_item, null);

			view.txtViewTitle = (TextView) convertView.findViewById(R.id.txtMenu);
			view.imgViewFlag = (ImageView) convertView.findViewById(R.id.iconMenu);

			convertView.setTag(view);
		}
		else
		{
			view = (ViewHolder) convertView.getTag();
		}

		MenuInfo menu = menuList.get(position);
		view.txtViewTitle.setText(menu.getName());
		view.imgViewFlag.setImageResource(menu.getResource());

		return convertView;
	}
}
